package com.ldc.framework.fragment

import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMFragment
import com.ldc.commonres.uts.TagUts
import com.ldc.framework.activity.ProjectsListActivity
import com.ldc.framework.adapter.ProjectAdapter
import com.ldc.framework.databinding.FrameworkFragmentWxBinding
import com.ldc.framework.model.ProjectClassModel
import com.ldc.framework.viewmodel.FrameworkViewModelBase


class WxFragment : VBMFragment<FrameworkFragmentWxBinding, FrameworkViewModelBase>() {
    private var projectAdapter: ProjectAdapter? = null

    companion object {
        private const val REFRESH_CODE = 0x124
        fun newInstance(): WxFragment = WxFragment().apply {
            arguments = Bundle().apply {
                //
            }
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getWxList()
            }
        }
        return super.processMessage(msg)
    }


    override fun main() {
        initAdapter()
        viewBind?.contentView?.refreshView?.also { rf ->
            rf.setOnRefreshListener {
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
            rf.setEnableLoadMore(false)

        }
        viewBind?.statusView?.let {
            it.showLoadView()
            it.addRetryClickListener(object : OnRowListener<Int> {
                override fun onRow(v: View, data: Int) {
                    it.showLoadView("正在重试，请稍等")
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)

    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FrameworkFragmentWxBinding = FrameworkFragmentWxBinding.inflate(inflater, container, false)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun getWxList() {
        viewModel?.WxList()?.observe(this) {
            viewBind?.statusView?.showContentView()
            viewBind?.contentView?.refreshView?.also { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                if (it.data.isNullOrEmpty()) {
                    viewBind?.statusView?.showEmptyView()
                } else {
                    projectAdapter?.newData(it.data)
                }
            } else {
                viewBind?.statusView?.showErrorView()
            }
        }
    }

    private fun initAdapter() {
        projectAdapter = ProjectAdapter().also {
            it.addOnRowListener(object : OnRowListener<ProjectClassModel.ProjectClassModelItem> {
                override fun onRow(v: View, data: ProjectClassModel.ProjectClassModelItem) {
                    ProjectsListActivity.start(this@WxFragment, data.name, "${data.id}")
                }
            })
        }
        viewBind?.contentView?.rv?.recyclerview?.also {
            it.adapter = projectAdapter
            it.setHasFixedSize(true)
            it.layoutManager = TagUts.getLayoutManager(it.context)
        }
    }


}